package org.blame.rabbitmqadvancedfeature.config;


import org.blame.rabbitmqadvancedfeature.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class DelayConfig {


    @Bean("delayQueue")
    public Queue delayQueue(){
        return QueueBuilder.durable(Constants.DELAY_QUEUE).build();
    }
    @Bean("delayExchange")
    public DirectExchange delayExchange(){
        return ExchangeBuilder.directExchange(Constants.DELAY_EXCHANGE).delayed().build();
    }

    @Bean("delayBlinding")
    public Binding delayBlinding(@Qualifier("delayQueue") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("delay");
    }









    //    使用ttl+死信队列模拟延迟队列
    @Bean("normalQueue2")
    public Queue normalQueue2(){
        return QueueBuilder.durable(Constants.NORMAL_QUEUE2)
//                .ttl(10000)
                .deadLetterExchange(Constants.DEL_EXCHANGE2)
                .deadLetterRoutingKey("del2")
                .build();
    }

    @Bean("normalExchange2")
    public DirectExchange normalExchange2(){
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE2).build();
    }

    @Bean("normalBlinding2")
    public Binding normalBlinding2(@Qualifier("normalQueue2") Queue queue,
                                  @Qualifier("normalExchange2") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("normal2");
    }

    @Bean("delQueue2")
    public Queue delQueue2(){
        return QueueBuilder.durable(Constants.DEL_QUEUE2).build();
    }

    @Bean("delExchange2")
    public DirectExchange delExchange2(){
        return ExchangeBuilder.directExchange(Constants.DEL_EXCHANGE2).build();
    }

    @Bean("delBlinding2")
    public Binding delBlinding2(@Qualifier("delQueue2") Queue queue,
                               @Qualifier("delExchange2") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("del2");
    }
}
